package org.dht.com.msg;

import java.util.HashMap;
import java.util.Iterator;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

/**
 * The <code>BigMesseage</code> encapsulates all services from big message.
 * <p>
 * Java class for BigMessage complex type.
 * 
 * <p>
 * The following schema fragment specifies the expected content contained within
 * this class.
 * 
 * <pre>
 * <complexType name="BigMessage">
 *   <complexContent>
 *     <extension base="{http://www.DHT-UQ.org/message}Message">
 *       <sequence>
 *         <element name="datas" type="{http://www.DHT-UQ.org/message}Datas"/>
 *       </sequence>
 *     </extension>
 *   </complexContent>
 * </complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "BigMessage", propOrder = { "datas" })
@XmlRootElement(name = "bigMessage")
public class BigMessageXML extends MessageXML implements BigMessage {

	/**
	 * Hash map of names with datas
	 */
	@XmlElement(required = true)
	@XmlJavaTypeAdapter(XmlAdapterDatasMap.class)
	private HashMap<String, byte[]> datas;

	/**
	 * Builds BigMessage.
	 * 
	 * @param messageType
	 *            Message Type
	 * @param destination
	 *            Destination name
	 * @param source
	 *            Source name
	 * @param param
	 *            Params
	 * @param datasBytes
	 *            Datas
	 * @throws MalformedMessageException
	 *             throw when an message is malformed
	 */
	public BigMessageXML(MessageType messageType, String destination,
			String source) {
		super(messageType, destination, source);

		datas = new HashMap<String, byte[]>();
	}

	/**
	 * Builds big message empty
	 */
	BigMessageXML() {
	}

	/**
	 * Builds BigMessage.
	 * 
	 * @param sequenceNumber
	 *            Sequence number
	 * @param sendType
	 *            Send type
	 * @param messageType
	 *            Message Type
	 * @param destination
	 *            Destination name
	 * @param source
	 *            Source name
	 * @param param
	 *            Params
	 * @param datasBytes
	 *            Datas
	 * @throws MalformedMessageException
	 *             throw when an message is malformed
	 */
	public BigMessageXML(long sequenceNumber, SendType sendType,
			MessageType messageType, String destination, String source) {
		super(sequenceNumber, sendType, messageType, destination, source);

		datas = new HashMap<String, byte[]>();
	}

	/**
	 * Gets data by name
	 * 
	 * @param name
	 *            Data name
	 * @return Data
	 */
	public byte[] getData(String name) {
		if (!datas.containsKey(name)) {
			throw new IllegalArgumentException("The big message type "
					+ messageType.getName() + " not contains param '" + name
					+ "'");
		} else {
			return datas.get(name);
		}
	}

	/**
	 * Gets the value of the datas property.
	 * 
	 * @return possible object is {@link Datas }
	 * 
	 */
	public HashMap<String, byte[]> getDatas() {
		return datas;
	}

	/**
	 * Sets the value of the datas property.
	 * 
	 * @param value
	 *            allowed object is {@link Datas }
	 * 
	 */
	public void setDatas(HashMap<String, byte[]> value) {
		this.datas = value;
	}

	/**
	 * Adds data to message
	 * 
	 * @param name
	 *            Data name
	 * @param data
	 *            Data value
	 */
	public void addData(String name, byte[] data) {
		datas.put(name, data);
	}

	/**
	 * Gets keys of datas
	 * 
	 * @return Data keys
	 */
	public Iterator<String> getDatasKey() {
		return datas.keySet().iterator();
	}

}
